﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 9/11/2011
-- Description:	Member Class
-- =============================================
*/

namespace BusinessLayer
{
   public class Member
    {
    #region "Variables and Properties"
    //Variables
        private int member_ID;        
        private string member_Email;        
        private string member_Password;        
        private string member_Name;        
        private string member_Address;        
        private string member_Tel;        
        private int status;        

    //Properties
        public int Member_ID
        {
            get { return member_ID; }
            set { member_ID = value; }
        }

        public string Member_Email
        {
            get { return member_Email; }
            set { member_Email = value; }
        }

        public string Member_Password
        {
            get { return member_Password; }
            set { member_Password = value; }
        }

        public string Member_Name
        {
            get { return member_Name; }
            set { member_Name = value; }
        }

        public string Member_Address
        {
            get { return member_Address; }
            set { member_Address = value; }
        }

        public string Member_Tel
        {
            get { return member_Tel; }
            set { member_Tel = value; }
        }

        public int Status
        {
            get { return status; }
            set { status = value; }
        }
    #endregion

    #region "Initial Methods"
        public Member() { }

        public Member(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                Member_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    Member_Email = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    Member_Password = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    Member_Name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    Member_Address = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    Member_Tel = sdr.GetString(5);
                if (!sdr.IsDBNull(6))
                    Status = sdr.GetInt32(6);
            }
        }
    #endregion

    #region "Bussiness Methods"
    //Get All Member
        public static List<Member> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Member_SHOWALL";
            List<Member> lstMember = new List<Member>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstMember.Add(new Member(sdr));
            }

            return lstMember;
        }

    //Get One Member
        public static Member GetOne(int idMember)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Member_SHOWONE";
            Member member = new Member();
            SqlDataReader sdr;

            dbh.AddParameter("@Member_ID", idMember);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                member.Member_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    member.Member_Email = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    member.Member_Password = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    member.Member_Name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    member.Member_Address = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    member.Member_Tel = sdr.GetString(5);
                if (!sdr.IsDBNull(6))
                    member.Status = sdr.GetInt32(6);
            }

            return member;
        }

    //Insert Member
        public static void Insert(Member member)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Member_INSERT";

            dbh.AddParameter("@Member_Email", member.Member_Email);
            dbh.AddParameter("@Member_Password", member.Member_Password);
            dbh.AddParameter("@Member_Name", member.Member_Name);
            dbh.AddParameter("@Member_Address", member.Member_Address);
            dbh.AddParameter("@Member_Tel", member.Member_Tel);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update Member
        public static void Update(Member member)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Member_UPDATE";

            dbh.AddParameter("@Member_ID", member.Member_ID);
            dbh.AddParameter("@Member_Email", member.Member_Email);
            dbh.AddParameter("@Member_Name", member.Member_Name);
            dbh.AddParameter("@Member_Address", member.Member_Address);
            dbh.AddParameter("@Member_Tel", member.Member_Tel);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete Member
        public static void Delete(int idMember)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Member_DELETE";

            dbh.AddParameter("@Member_ID", idMember);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    //Lost Password
        public static void LostPass(string email, string name, string tel)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Member_Email", email);
            dbh.AddParameter("@Member_Name", name);
            dbh.AddParameter("@Member_Tel", tel);
            string cmd = "tbl_Member_LostPass";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    //Login
        public static Member Login(string email, string pass)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Member_Email", email);
            dbh.AddParameter("@Member_Password", pass);
            string cmd = "tbl_Member_LOGIN";
            Member member = new Member();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                member.Member_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    member.Member_Email = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    member.Member_Password = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    member.Member_Name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    member.Member_Address = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    member.Member_Tel = sdr.GetString(5);
                if (!sdr.IsDBNull(6))
                    member.Status = sdr.GetInt32(6);
            }

            return member;
        }

    #endregion
    }
}
